/**
 * @file     wordPattern.ts
 * @brief    [290. 单词规律](https://leetcode.cn/problems/word-pattern/)
 * @author   Zhu
 * @date     2022-11-05 13:59
 */

function wordPattern(pattern: string, s: string): boolean {
    const words = s.split(' ');
    const patternMap = {};
    const wordMap = {};
    if (words.length !== pattern.length) return false;
    for (let i = 0; i < pattern.length; i++) {
        if (patternMap[pattern[i]]) {
            if (patternMap[pattern[i]] !== words[i]) return false;
        } else {
            if (wordMap[words[i]]) return false;
            patternMap[pattern[i]] = words[i];
            wordMap[words[i]] = pattern[i];
        }
    }

    return true;
}
